
package  summitsoft.android.hiwifi.base;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;

//a class that manager wifi hot spots
public class WifiNetwork{
	private Context mContext;
	
	// Most recent WiFi scan results.  Null if no data.
	private List<ScanResult> mWifiSignals = null;
	// The WiFi manager, for WiFi state updates.
	private WifiManager mWifiManager;
	// Current WiFi power state and connection state.
	private int mWifiPowerState = WifiManager.WIFI_STATE_UNKNOWN;
	
	// Time at which we last started a WiFi scan, and the time we last
	// got scan data.
	private long mWifiScanTime = 0;
	private long mWifiSignalsTime = 0;
	
	public WifiNetwork(Context context){
		mContext = context;
		mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
	}
	
	//start wifi manager
	public void start(){
		 // Register for the intents that tell us about WiFi state changes.
        // Thanks to Kent for the tip.
        IntentFilter wifiFilter = new IntentFilter();
        wifiFilter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION);
        wifiFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        wifiFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
        wifiFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        wifiFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        wifiFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        wifiFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mContext.registerReceiver(wifiListener, wifiFilter);

        // Get the initial status, or any status that may have changed
		// while we were paused.
		getWifiState();
		getWifiConnection();
	}
	
	public void stop(){
	}
	
	public void refresh(long time){
		if (mWifiPowerState == WifiManager.WIFI_STATE_ENABLED) {
			long age = (time - mWifiSignalsTime) / 1000;
			// Scan for WiFi networks every WIFI_SCAN_INTERVAL secs at most.
			long scan = (time - mWifiScanTime) / 1000;
			if (age > WifiConst.WIFI_SCAN_INTERVAL && scan > WifiConst.WIFI_SCAN_INTERVAL) {
				mWifiManager.startScan();
				mWifiScanTime = time;
			}
		}
	}
	
	private void getWifiConnection() {
		synchronized (mWifiManager){
			
		}
	}
	
	//notify the app about the scan results
	private void getScanResults() {
		synchronized (mWifiManager) {
			mWifiSignals = mWifiManager.getScanResults();
			mWifiSignalsTime = System.currentTimeMillis();

			for (ScanResult scan : mWifiSignals) {
				// Convert the value to ASU and display it. See note
				// in the constructor.
				// dBm = -113 + 2*asu
				final int asu = Math.round((scan.level + 113f) / 2f);

				final String freq = WifiConst.format53(scan.frequency / 1000f);
				final String value = (asu < 10 ? " " : "") + asu;
				Log.d("wifi", "SSID =" + scan.SSID + " capabilities="
						+ scan.capabilities + " BSSID=" + scan.BSSID);

			}
		}
	}
	
	private void getWifiState(){
		synchronized (mWifiManager) {
			mWifiPowerState = mWifiManager.getWifiState();
			if (mWifiPowerState == mWifiManager.WIFI_STATE_ENABLED) {
				long time = System.currentTimeMillis();
				long age = (time - mWifiSignalsTime) / 1000;
				long scan = (time - mWifiScanTime) / 1000;
				if (age > WifiConst.WIFI_SCAN_INTERVAL && scan > WifiConst.WIFI_SCAN_INTERVAL) {
					mWifiManager.startScan();
					mWifiScanTime = time;
				}
			}
		}
	}
	
	/**
	 * The BroadcastReceiver receives the intent
	 * mWifiManager.SCAN_RESULTS_AVAILABLE_ACTION, as set up in our
	 * constructor.  This tells us (and anyone else who cares) that a
	 * WiFi scan has completed.  We can get the results from
	 * mWifiManager.getScanResults().
	 */
	private BroadcastReceiver wifiListener = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			final String action = intent.getAction();
	        if (action.equals(WifiManager.NETWORK_IDS_CHANGED_ACTION))
	        	;
	        else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
	        	getWifiConnection();
	        else if (action.equals(WifiManager.RSSI_CHANGED_ACTION))
	        	getWifiConnection();
	        else if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
	        	getScanResults();
	        else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
	        	getWifiConnection();
	        else if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION))
	        	getWifiConnection();
	        else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
	        	getWifiState();
		}
	
	};

}